<?php

/**
 * This file is part of the Pagizer package.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @copyright   Copyright (c) 2010 Advisa (http://www.advisa.fr)
 * @author      Pagizer Core Team <team@pagizer.org>
 * @package     pagizer
 * @subpackage  website
 */

/**
 * This patch will transform old block actions with new one.
 */
class core_module_website_0_3_0_Patch001 extends f_package_Patch
{
    public function patch()
    {
        $docProvider = $this->getDocumentProvider();

        $pages = $docProvider->loadDocuments('website/page')->retrieveDocuments();

        if(count($pages) > 0)
        {
            $tempDocService = m_backoffice_services_TempDocService::getInstance();
            
            foreach($pages as $page)
            {
                /* @var $page m_website_documents_page */
                
                if($tempDocService->hasTemporary($page->getUniqueId(), $page->getLang()))
                {
                    $tempPage = $tempDocService->getTemporaryDocument($page->getUniqueId(), $page->getLang());
                    $tempPage = $this->setBlockParams($tempPage);
                    $tempDocService->saveTemporaryDocument($tempPage);
                }

                $page = $this->setBlockParams($page);
                $page->save(false);
            }
        }
    }

    private function setBlockParams($page)
    {
        $blocks = $page->getAllBlocks();

        if(count($blocks) > 0)
        {
            foreach ($blocks as $k => $blockValue)
            {
                if($blockValue['action'] == 'displayMenu')
                {
                    $blockValue['action'] = 'front_Menu';
                }

                if($blockValue['action'] == 'loremIpsum')
                {
                    $blockValue['action'] = 'front_LoremIpsum';
                }

                if($blockValue['action'] == 'freeContent')
                {
                    $blockValue['action'] = 'front_FreeContent';
                }

                if($blockValue['action'] == 'sitemap')
                {
                    $blockValue['action'] = 'front_SiteMap';
                }

                unset($blockValue['rawValues']);

                $page->setBlockParamsById($k, $blockValue);
            }
        }

        return $page;
    }
}